\chapter{舵机的使用 }
\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor}{}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor}\index{舵机的使用@{舵机的使用}}
\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor_autotoc_md80}%
\Hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor_autotoc_md80}%




\href{mailto:panrui@hnu.edu.cn}{\texttt{panrui@hnu.\+edu.\+cn}}

\begin{quote}
todo\+: 由于新增了bsp\+\_\+pwm的支持,舵机模块需要部分重构 \end{quote}
\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor_autotoc_md81}{}\doxysubsection{\texorpdfstring{舵机基础知识}{舵机基础知识}}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor_autotoc_md81}
已最常见的\+SG90舵机为例，\+SG90舵机要求工作在频率为50\+HZ——周期为20ms的\+PWM波，且对应信号的高低电平在0.5ms -\/ 2.\+5ms之间，对应的舵机转动角度如下表所示（当然也可以按照这个线性的对应关系去达到转动自己想要的角度，如想要转动60°，则高电平脉宽为大概为1.2ms，具体能不能转到特定的角度还和舵机的精度有关）

\texorpdfstring{$>$}{>}0.\+5ms-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/---0度； 2.\+5\% \texorpdfstring{$>$}{>}1.\+0ms-\/-\/-\/-\/-\/-\/-\/-\/-\/---45度； 5.\+0\% \texorpdfstring{$>$}{>}1.\+5ms-\/-\/-\/-\/-\/-\/-\/-\/-\/---90度； 7.\+5\% \texorpdfstring{$>$}{>}2.\+0ms-\/-\/-\/-\/-\/-\/-\/-\/---135度； 10.\+0\% \texorpdfstring{$>$}{>}2.\+5ms-\/-\/-\/-\/-\/-\/-\/-\/---180度； 12.\+5\%

根据{\ttfamily \texorpdfstring{$<$}{<}font color=black size=3\texorpdfstring{$>$}{>}Tout = (PSC+1)\texorpdfstring{$\ast$}{*} (ARR+1)/\+Tclk\texorpdfstring{$<$}{<}/font\texorpdfstring{$>$}{>}}公式 则我们需要产生50\+Hz的\+PWM波，则预分频的系数为 Prescaler = 168-\/1，自动重装载值 Counter Period = 20000-\/1，此时定时器产生的频率为 168Mhz/168/20000 = 50Hz。 当然这个值也可以自己设置，只要满足产生的频率为50\+Hz即可. {\ttfamily \doxylink{group___t_i_m___exported___macros_ga300d0c9624c3b072d3afeb7cef639b66}{\+\_\+\+\_\+\+HAL\+\_\+\+TIM\+\_\+\+SET\+\_\+\+COMPARE(htim, Channel, compare\+\_\+value)};} 这是设置占空比的函数 eg：当初始占空比为1200/20000则为6，根据20\texorpdfstring{$\ast$}{*}6\%=1.\+2ms （1.2-\/0.\+5）/(2.\+5-\/0.\+5)\texorpdfstring{$\ast$}{*}180=63° 故舵机会转动63° 为了方便通过上述eg我们将所需要的角度与\+PWM计数值对应关系封装成函数。需要在初始化的時候输入我们所需要的角度和相关定时器参数即可。这样我们就可以设置\+SG90为参数范围内(0\texorpdfstring{$\sim$}{\string~}180°)任意度数。

\DoxyHorRuler{0}
 \texorpdfstring{$<$}{<}\texorpdfstring{$<$}{<}\texorpdfstring{$<$}{<}\texorpdfstring{$<$}{<}\texorpdfstring{$<$}{<}\texorpdfstring{$<$}{<}\texorpdfstring{$<$}{<} HEAD\hypertarget{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor_autotoc_md83}{}\doxysection{\texorpdfstring{如何注册一个舵机实例}{如何注册一个舵机实例}}\label{md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor_autotoc_md83}
!!!

{\bfseries{注意！由于舵机为开环控制，无论选择舵机为何种类型，舵机都能够正常运行，但是运行的角度可能会与设定不同，请务必正确选择舵机型号！且最多添加7个舵机！}} 我们可以像这样注册一个舵机实例 
\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{struct_servo_instance}{ServoInstance}}\ *leftservomoto;}
\DoxyCodeLine{\textcolor{comment}{//初始化参数}}
\DoxyCodeLine{\mbox{\hyperlink{struct_servo___init___config__s}{Servo\_Init\_Config\_s}}\ config=\{}
\DoxyCodeLine{\ \ \ \ \textcolor{comment}{//舵机安装选择的定时器及通道}}
\DoxyCodeLine{\ \ \ \ \textcolor{comment}{//C板有常用的7路PWM输出:TIM1-\/1,2,3,4\ TIM8-\/1,2,3}}
\DoxyCodeLine{\ \ \ \ .htim=\&htim1,}
\DoxyCodeLine{\ \ \ \ .Channel=\mbox{\hyperlink{group___t_i_m___channel_ga6b1541e4a49d62610899e24bf23f4879}{TIM\_CHANNEL\_1}},}
\DoxyCodeLine{\ \ \ \ \textcolor{comment}{//舵机的初始化模式和类型}}
\DoxyCodeLine{\ \ \ \ .Servo\_Angle\_Type=Start\_mode,}
\DoxyCodeLine{\ \ \ \ .Servo\_type=Servo180,}
\DoxyCodeLine{\};}
\DoxyCodeLine{\textcolor{comment}{//\ 设置好参数后进行初始化并保留返回的指针}}
\DoxyCodeLine{leftservomoto\ =\ ServoInit(\&config);}

\end{DoxyCode}
 \texorpdfstring{$>$}{>}要控制一个舵机 我们提供了以下三个接口 
\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{comment}{//自由模式下，写入自由角度数值}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ \mbox{\hyperlink{servo__motor_8h_ac4157b72fa36b65b683660b86b9da612}{Servo\_Motor\_FreeAngle\_Set}}(\mbox{\hyperlink{struct_servo_instance}{ServoInstance}}\ *Servo\_Motor,\ int16\_t\ S\_angle);}
\DoxyCodeLine{\textcolor{comment}{//起止模式下，写入起始，终止角度数值(防止反复写入起始和终止角度)}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ Servo\_Motor\_StartSTOP\_Angle\_Set(\mbox{\hyperlink{struct_servo_instance}{ServoInstance}}\ *Servo\_Motor,\ int16\_t\ Start\_angle,\ int16\_t\ Final\_angle);}
\DoxyCodeLine{\textcolor{comment}{/*}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ Free\_Angle\_mode,\ //\ 任意角度模式}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ Start\_mode,\ \ \ \ \ \ //\ 起始角度模式}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ Final\_mode,\ \ \ \ \ \ //\ 终止角度模式}}
\DoxyCodeLine{\textcolor{comment}{*/}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ \mbox{\hyperlink{servo__motor_8h_a871a49699fa114b54020843e1e149f25}{Servo\_Motor\_Type\_Select}}(\mbox{\hyperlink{struct_servo_instance}{ServoInstance}}\ *Servo\_Motor,int16\_t\ mode);}
\DoxyCodeLine{\textcolor{comment}{//比如我们要使用舵机，并更改一个舵机的模式}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ ServoTask()}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{comment}{//更改leftservomoto为Free\_Angle\_mode模式}}
\DoxyCodeLine{\ \ \ \ \mbox{\hyperlink{servo__motor_8h_a871a49699fa114b54020843e1e149f25}{Servo\_Motor\_Type\_Select}}(leftservomoto,Free\_Angle\_mode);}
\DoxyCodeLine{\ \ \ \ \textcolor{comment}{//设置转到0角度}}
\DoxyCodeLine{\ \ \ \ \mbox{\hyperlink{servo__motor_8h_ac4157b72fa36b65b683660b86b9da612}{Servo\_Motor\_FreeAngle\_Set}}(leftservomoto,\ 0);}
\DoxyCodeLine{\ \ \ \ \textcolor{comment}{//调用函数，控制电机}}
\DoxyCodeLine{\ \ \ \ Servo\_Motor\_Control();}
\DoxyCodeLine{\}}

\end{DoxyCode}
 \DoxyHorRuler{0}
 \begin{quote}
\begin{quote}
\begin{quote}
\begin{quote}
\begin{quote}
\begin{quote}
\begin{quote}
0053f7523b5b581171719f5d4276ee4d31880b98 \end{quote}
\end{quote}
\end{quote}
\end{quote}
\end{quote}
\end{quote}
\end{quote}
